<?php
class Member_Model extends CI_Model{

	public function __construct(){
		parent::__construct();
		$this->db 		= $this->load->database('default', TRUE);
	}
	private $num = 0;
	public function checkUser($ref_id=null,$ref_pass=null){
		$data = $this->db->get_where("member",array('user_id' => $ref_id,'pass_id'=>$ref_pass,'delete_flag'=>0))->result_array();
		$result = (count($data)>0)?true:false;
		return $result;
	}
	public function checkExistEmail($ref = null){
		$data = $this->db->get_where("member",array('email' => $ref,'delete_flag'=>0))->result_array();
		$result = (count($data)>0)?true:false;
		return $result;
	}
	public function checkExistUser($ref = null){
		$data = $this->db->get_where("member",array('user_id' => $ref))->result_array();
		$result = (count($data)>0)?true:false;
		return $result;
	}
	public function saveUserNew($ref = null){
		$this->db->insert('member', $ref);
		return $this->db->affected_rows();
	}
	public function checkUserKeyRegist($key_regist = null,$user = null){
		$data = $this->db->get_where("member",array('key_regist' => $key_regist,'user_id'=>$user,'delete_flag'=>1))->result_array();
		$result = (count($data)>0)?true:false;
		return $result;
	}
	public function updateUserRegistFinish($keyregist =null,$user =null){
		$this->db->update('member', array('delete_flag'=>0), array('user_id'=>$user,'key_regist'=>$keyregist,'delete_flag'=>1));
		return $this->db->affected_rows();
	}
	public function updateUserPassForgot($email =null){
		$this->db->update('member', array('pass_id'=>md5('ThanhNV12345678')), array('email'=>$email,'delete_flag'=>0));
		return $this->db->affected_rows();
	}
	public function getUserForgot($email=null){
		$this->db->select('user_id,email');
		$this->db->where(array('delete_flag'=>0,'email'=>$email));
		$data = $this->db->get('member')->result_array();
		return $data;
	}
	public function checkExistRecoverMail($ref = null){
		$data = $this->db->get_where("member",array('email' => $ref,'delete_flag'=>0,'pass_id'=>md5('ThanhNV12345678')))->result_array();
		$result = (count($data)>0)?true:false;
		return $result;
	}
	public function showMemberProduct($ref = null){
		$this->db->select('cat_id, cat_name, images,cat_price,cat_size');
		$this->db->where(array('delete_flag'=>0,'cat_type_sub_id'=>$ref));
		$this->db->order_by("cat_type_sub_sort_order asc, update_date desc");
		$data = $this->db->get('member_category',6, 0)->result_array();
		return $data;
	}
	public function showDetailId($ref = null){
		$this->db->select('cat_name, images, cat_description, cat_demension, cat_assembly, cat_material, cat_color, cat_price');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$ref));
		$data = $this->db->get('member_category')->result_array();
		return $data;
	}
	public function showRelationId($ref = null){
		//ten cat_name, images, cat_description, cat_demension, cat_assembly, cat_material, cat_color, cat_price, cat_id, cat_type_sub_id
		$this->db->select('cat_type_sub_id');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$ref));
		$data = $this->db->get('member_category')->result_array();
		$sub_id = $data[0]['cat_type_sub_id'];
		$this->db->select('cat_id, cat_name, images,cat_price,cat_size');
		$this->db->where(array('delete_flag'=>0,'cat_type_sub_id'=>$sub_id));
		$this->db->order_by("cat_type_sub_sort_order asc, update_date desc");
		$data = $this->db->get('member_category',12, 0)->result_array();
		return $data;
	}
	public function getSubcateId($ref = null,$limit = 10, $offset = 0){
		$this->db->select('cat_id, cat_name, images,cat_price,cat_size');
		$this->db->where(array('delete_flag'=>0,'cat_type_sub_id'=>$ref));
		$this->db->order_by("cat_type_sub_sort_order asc, update_date desc");
		$data = $this->db->get('member_category',$limit, $offset)->result_array();
		return $data;
	}
	public function count_all($id = null){
		$data = $this->db->get_where('member_category',array('cat_type_sub_id'=>$id,'delete_flag'=>0));
		return $data->num_rows();
	}
	public function count_condition(){
		return $this->num;
	}
	public function searchMember($ref = null,$limit = 10, $offset = 0){
		$sql = '';
		$sql.= "select distinct mb.images, mb.cat_name, mb.cat_id,mb.cat_price,mb.cat_size from member_category as mb, common_category as cm 
		where mb.delete_flag = 0 and cm.delete_flag = 0 and  
		cm.cat_description like '%".$ref."%' or 
		cm.cat_demension like '%".$ref."%' or 
		cm.cat_assembly like '%".$ref."%' or 
		cm.cat_material like '%".$ref."%' or 
		cm.cat_name like '%".$ref."%' or 
		cm.cat_code like '%".$ref."%' or 
		mb.cat_description like '%".$ref."%' or 
		mb.cat_demension like '%".$ref."%' or 
		mb.cat_assembly like '%".$ref."%' or 
		mb.cat_material like '%".$ref."%' or 
		mb.cat_name like '%".$ref."%' or 
		mb.cat_code like '%".$ref."%' or 
		mb.cat_code like '%".$ref."%'
		ORDER BY cm.cat_type_sub_sort_order ASC , cm.update_date DESC LIMIT ".$offset." , ".$limit;
		$data = $this->db->query($sql)->result_array();
		return $data;
	}
	public function countMember($ref){
		$sql = '';
		$sql.= "select distinct mb.images, mb.cat_name, mb.cat_id from member_category as mb, common_category as cm 
		where mb.delete_flag = 0 and cm.delete_flag = 0 and  
		cm.cat_description like '%".$ref."%' or 
		cm.cat_demension like '%".$ref."%' or 
		cm.cat_assembly like '%".$ref."%' or 
		cm.cat_material like '%".$ref."%' or 
		cm.cat_name like '%".$ref."%' or 
		cm.cat_code like '%".$ref."%' or 
		mb.cat_description like '%".$ref."%' or 
		mb.cat_demension like '%".$ref."%' or 
		mb.cat_assembly like '%".$ref."%' or 
		mb.cat_material like '%".$ref."%' or 
		mb.cat_name like '%".$ref."%' or 
		mb.cat_code like '%".$ref."%' or 
		mb.cat_code like '%".$ref."%'
		ORDER BY cm.cat_type_sub_sort_order ASC , cm.update_date DESC";
		return $this->db->query($sql)->num_rows();
	}
	public function searchNotMember($ref = null,$limit = 10, $offset = 0){
		$sql = '';
		$sql.= "select distinct cm.images, cm.cat_name, cm.cat_id,cm.cat_price,cm.cat_size from common_category as cm 
		where cm.delete_flag = 0 and  
		cm.cat_description like '%".$ref."%' or 
		cm.cat_demension like '%".$ref."%' or 
		cm.cat_assembly like '%".$ref."%' or 
		cm.cat_material like '%".$ref."%' or 
		cm.cat_name like '%".$ref."%' or 
		cm.cat_code like '%".$ref."%'
		ORDER BY cm.cat_type_sub_sort_order ASC , cm.update_date DESC
		limit ".$offset.", ".$limit;
		$data = $this->db->query($sql)->result_array();
		return $data;
	}
	public function countNotMember($ref){
		$sql = '';
		$sql.= "select distinct cm.images, cm.cat_name, cm.cat_id,cm.cat_price,cm.cat_size from common_category as cm 
		where cm.delete_flag = 0 and  
		cm.cat_description like '%".$ref."%' or 
		cm.cat_demension like '%".$ref."%' or 
		cm.cat_assembly like '%".$ref."%' or 
		cm.cat_material like '%".$ref."%' or 
		cm.cat_name like '%".$ref."%' or 
		cm.cat_code like '%".$ref."%'
		ORDER BY cm.cat_type_sub_sort_order ASC , cm.update_date DESC";
		return $this->db->query($sql)->num_rows();
	}
}